package com.etc.myblog.biz;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.etc.myblog.dao.ArticleDao;
import com.etc.myblog.dao.ScolumnDao;
import com.etc.myblog.entity.Scolumn;
import com.etc.myblog.util.PageUtil;
import com.etc.myblog.util.SessionFactoryUtil;

public class ScolumnBiz {
	//专栏分页
	public PageUtil<Scolumn> findPageCol(int pageIndex, int pageSize) {
		PageUtil<Scolumn> pUser = new PageUtil<Scolumn>();
		List<Scolumn> list = new ArrayList<Scolumn>();
		int count = 0;
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			list = dao.findPageCol((pageIndex-1)*pageSize, pageSize);
			count = dao.findColCount();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			session.close();
		}
		
		pUser.setPageSize(pageSize);
		pUser.setPageNumber((int)Math.ceil((double)count/pageSize));
		pUser.setIndex(pageIndex);
		pUser.setCount(count);
		pUser.setList(list);
		
		return pUser;
	}
	//随机一个专栏
	public Scolumn findRandColumn() {
		Scolumn rand = new Scolumn();
		SqlSession session = SessionFactoryUtil.getSession(); 
		try {
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			rand = dao.findRandColumn();
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			session.close();
		}
		return rand;
	}
	
	
	//随机10个专栏
	public List<Scolumn> findRandColTen() {
		List<Scolumn> randTen = new ArrayList<Scolumn>();
		SqlSession session = SessionFactoryUtil.getSession(); 
		try {
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			randTen = dao.findRandColTen();
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			session.close();
		}
		return randTen;
	}
	//根据专栏id获取专栏
	public Scolumn findById(int columnId) {
		Scolumn scolumn = new Scolumn();
		SqlSession session = SessionFactoryUtil.getSession(); 
		try {
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			scolumn = dao.findById(columnId);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			session.close();
		}
		return scolumn;
	}
	
	//根据用户id获取专栏
	public List<Scolumn> findByUserId(int userId) {
		List<Scolumn> scolumn = new ArrayList<Scolumn>();
		SqlSession session = SessionFactoryUtil.getSession(); 
		try {
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			scolumn = dao.findByUserId(userId);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			session.close();
		}
		return scolumn;
	}
	
	//获取专栏的文章数目
	public int articleCount(int columnId){
		int count = 0;
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			count = dao.articleCount(columnId);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		return count;
	}
	
	//获取专栏的文章总浏览次数
	public int articleView(int columnId) {
		int sum = 0;
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			sum = dao.articleView(columnId);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		return sum;
	}
	
	//查找所有的专栏
	public List<Scolumn> find(){
		List<Scolumn> list = new ArrayList<Scolumn>();
		SqlSession session = SessionFactoryUtil.getSession();
		try{
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			list = dao.find();
		}catch(Exception e){
			System.out.println(e.getMessage());
		}finally{
			session.close();
		}
		return list;
	}
	//查找没有管理的专栏
	public List<Scolumn> findNoAdSColumn(){
		List<Scolumn> list = new ArrayList<Scolumn>();
		SqlSession session = SessionFactoryUtil.getSession();
		try{
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			list = dao.findNoAdSColumn();
		}catch(Exception e){
			System.out.println(e.getMessage());
		}finally{
			session.close();
		}
		
		return list;
	}
	//修改专栏状态
	public void updateStatus(Scolumn sCol) {
	     SqlSession session = SessionFactoryUtil.getSession();
		try{
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			dao.updateStatus(sCol);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
			
	}
	
	//根据用户id获取所有专栏
	public List<Scolumn> findAllByUserId(int userId){
	List<Scolumn> scolumn = new ArrayList<Scolumn>();
	SqlSession session = SessionFactoryUtil.getSession(); 
	try {
		ScolumnDao dao = session.getMapper(ScolumnDao.class);
		scolumn = dao.findAllByUserId(userId);
	} catch (Exception e) {
		System.out.println(e.getMessage());
	} finally {
		session.close();
	}
	return scolumn;
	}
	
	public void updateNopic(Scolumn scolumn) {
		SqlSession session = SessionFactoryUtil.getSession(); 
		try {
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			dao.updateNopic(scolumn);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			session.close();
		}
	}
	
	public void update(Scolumn scolumn) {
		SqlSession session = SessionFactoryUtil.getSession(); 
		try {
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			dao.update(scolumn);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			session.close();
		}
	}
	public void add(Scolumn scolumn) {
		SqlSession session = SessionFactoryUtil.getSession(); 
		try {
			ScolumnDao dao = session.getMapper(ScolumnDao.class);
			dao.add(scolumn);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			session.close();
		}
	}
}
